/**
 * Created by wzl on 2017/4/23.
 */
import {Component} from 'react';
import {geom2VecArr} from '../util/util';
import * as THREE from 'three';

class Landcover extends Component {
    constructor(props){
        super(props);
        this.state={
            object:new THREE.Object3D()
        }
    }
    componentDidMount(){
        const {layer, tileObject, tileSize} = this.props;
        if(layer){
            let extent = layer.extent;
            let len = layer.length;
            // let object = new THREE.Object3D();
            for (let i = 0; i < len; i++) {
                let feature = layer.feature(i);
                let geom = feature.loadGeometry();//直接用GeoJSON

                let vecArr = geom2VecArr(geom[0], extent, tileSize);
                // console.log(vecArr);
                let shape = new THREE.Shape(vecArr);

                let geometry = new THREE.ShapeGeometry( shape );
                let material = new THREE.MeshBasicMaterial({
                    color: new THREE.Color("hsl(75, 62%, 81%)"),
                    polygonOffset:true,
                    polygonOffsetFactor:0.5,
                    depthTest: false
                });

                let mesh = new THREE.Mesh(geometry, material);

                this.state.object.add(mesh);
            }
            tileObject.add(this.state.object);
        }
    }
    componentWillUnmount(){
        const {tileObject} = this.props;
        if(tileObject && this.state.object){
            tileObject.remove(this.state.object);
        }
    }
    render(){
        return null;
    }
}

export default Landcover;